package jmine.tec.report.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import jmine.tec.report.api.ReportRenderer;
import jmine.tec.utils.register.Receiver;

/**
 * Implementação abstrata de registro de renderers relativos a relatórios.
 * 
 * @author lundberg
 * @param <R>
 */
public class AbstractReportRendererRegistry<R extends ReportRenderer> implements Receiver<R>, Serializable {

    private Map<String, R> strategyMap;

    private Class<R> allowedClass;

    /**
     * Construtor
     * 
     * @param allowedClass superclass dos renderers que devem ser aceitos
     */
    public AbstractReportRendererRegistry(Class<R> allowedClass) {
        this.strategyMap = new HashMap<String, R>();
        this.allowedClass = allowedClass;
    }

    /**
     * {@inheritDoc}
     */
    public void register(Collection<? extends R> registers) {
        for (R renderer : registers) {
            this.strategyMap.put(renderer.getRendererType(), this.allowedClass.cast(renderer));
        }
    }

    /**
     * Encontra o renderer para o report type dado
     * 
     * @param reportType report type
     * @return Renderer
     */
    public R getRenderer(String reportType) {
        if (this.strategyMap.containsKey(reportType)) {
            return this.strategyMap.get(reportType);
        } else {
            throw new UnsupportedOperationException(String.format("Renderer não definido para o tipo %s.", reportType));
        }
    }
}
